package com.zjeav.lingjiao.base.comon;

import com.zjeav.lingjiao.base.response.AccessToken;
import com.zjeav.lingjiao.base.service.DownLoadServices;
import com.zjeav.lingjiao.base.service.WXAccessTokenService;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;

public class CommonModel {
    public void downLoad(String url, final int type, final OnDownLoadListener loadListener) {
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        DownLoadServices downLoadServices = retrofit.create(DownLoadServices.class);
        downLoadServices.downloadFileWithDynamicUrlAsync(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseBody body) {
                        loadListener.OnDownLoadSuccess(body, type);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        loadListener.OnDownLoadFailure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getToken(String appid, String secret, String code, String grant_type, final OnAccessTokenListener listener) {

        Retrofit retrofit = RetrofitInstance.getAccessIntance();
        WXAccessTokenService service = retrofit.create(WXAccessTokenService.class);
        service.getAccessToken(appid, secret, code, grant_type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AccessToken>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(AccessToken token) {
                        listener.OnGetTokenSuccess(token);
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.OnGetTokenFail(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    interface OnAccessTokenListener {
        void OnGetTokenSuccess(AccessToken token);

        void OnGetTokenFail(Throwable throwable);
    }

    interface OnDownLoadListener {
        void OnDownLoadSuccess(ResponseBody body, int type);

        void OnDownLoadFailure(Throwable throwable);
    }
}
